#include "trackseditor.h"
#include "ui_trackseditor.h"

#include <algorithm>
#include <map>
#include <QVBoxLayout>
#include <mlgesture.h>
#include <gestureutils.h>

using namespace mlgesture;

TracksEditor::TracksEditor(vector<Trajectory> *trajectories, QWidget *parent) :
    QDialog(parent),
	ui_(new Ui::TracksEditor),
	trajectories_(trajectories)
{
	ui_->setupUi(this);
	Trajectory::getTrajectoryTypes(*trajectories, types_);

	QVBoxLayout *layout = new QVBoxLayout();
	QWidget *widget = new QWidget;
	widget->setLayout(layout);
	ui_->scrollArea->setWidget(widget);
	for (vector<Trajectory>::iterator it = trajectories->begin(); it != trajectories->end(); ++it) {
		it->type_ = QString::fromStdString(it->type_).toLower().toStdString();
		TrajectoryWidget *w = new TrajectoryWidget(*it, types_, this);
		widgets_.push_back(w);
		layout->addWidget(w);
		connect(w, SIGNAL(valueChanged()), this, SLOT(updateStatistics()));
	}
	ui_->filtrCombo->addItem(tr("Brak"));
	for (vector<string>::const_iterator it = types_.begin(); it != types_.end(); ++it) {
		ui_->filtrCombo->addItem(QString::fromStdString(*it));
	}

	connect(ui_->addTypeButton, SIGNAL(clicked()), this, SLOT(addCategory()));
	connect(this, SIGNAL(accepted()), this, SLOT(updateTypes()));
	updateStatistics();
	connect(ui_->addTypeButton, SIGNAL(clicked()), this, SLOT(updateStatistics()));
	connect(ui_->filtButton, SIGNAL(clicked()), this, SLOT(changeFilter()));
}

TracksEditor::~TracksEditor()
{
    delete ui_;
}

void TracksEditor::addCategory() {
	QString type = ui_->typeEdit->text().toLower();
	if (!type.isEmpty()) {
		types_.push_back(type.toStdString());
		for (vector<TrajectoryWidget*>::iterator it = widgets_.begin(); it != widgets_.end(); ++it) {
			(*it)->addType(type);
		}
	}
	ui_->filtrCombo->addItem(type);
}

void TracksEditor::updateTypes() {
	vector<TrajectoryWidget*>::iterator itWidget = widgets_.begin();
	for (vector<Trajectory>::iterator it = trajectories_->begin(); it != trajectories_->end(); ++it, ++itWidget) {
		it->type_ = (*itWidget)->getType().toLower().toStdString();
	}
	itWidget = widgets_.begin();
	for (vector<Trajectory>::iterator it = trajectories_->begin(); it != trajectories_->end(); ++itWidget) {
		if ((*itWidget)->isDeleted()) {
			it = trajectories_->erase(it);
		}
		else ++it;
	}
}

void TracksEditor::updateStatistics() {
	map<string, int> stats;
	vector<string>::iterator it_end = types_.end();
	for (vector<string>::iterator it = types_.begin(); it != it_end; ++it) {
		stats[*it] = 0;
	}
	for (vector<TrajectoryWidget*>::iterator it = widgets_.begin(); it != widgets_.end(); ++it) {
		if (!(*it)->isDeleted())
			stats[(*it)->getType().toStdString()]++;
	}
	QString text = tr("Statystyka:\n");
	int i = 0;
	for (map<string,int>::iterator it = stats.begin(); it != stats.end(); ++it, ++i) {
		text += tr("\t") + QString::fromStdString(it->first) + tr(": ") + QString::number(it->second);
		text += (i%2) ? tr("\n") : tr("\t");
	}
	ui_->statisticLabel->setText(text);
}

void TracksEditor::changeFilter() {
	QString type = ui_->filtrCombo->currentText().toLower();
	for (vector<TrajectoryWidget*>::iterator itWidget = widgets_.begin(); itWidget != widgets_.end(); ++itWidget) {
		if (ui_->filtrCombo->currentIndex() == 0 || type == (*itWidget)->getType().toLower()) {
			if (!(*itWidget)->isVisible())
				(*itWidget)->show();
		}
		else {
			(*itWidget)->hide();
		}
	}


}
